﻿// Type: trx2html.Parser.HtmlConverter
// Assembly: RidoTasks, Version=0.7.3.1523, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Users\jigar\Desktop\Vinay\trx2html_0.7\0.7.3\RidoTasks.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace trx2html
{
  internal class HtmlConverter
  {
    private TestRunResult run;

    public HtmlConverter(TestRunResult testRunResult)
    {
      this.run = testRunResult;
    }

    public string GetHtml()
    {
      StringBuilder result = new StringBuilder();
      this.WriteHeader(this.run, result);
      this.WriteBody(this.run, result);
      this.WriteEnvironmentInfo(this.run, result);
      this.WriteFooter(this.run, result);
      return ((object) result).ToString();
    }

    private void WriteHeader(TestRunResult run, StringBuilder result)
    {
      result.AppendFormat("<html><head><title>{0}</title>", (object) run.Name);
      result.Append("<style type='text/css'>");
      result.Append(this.GetCss());
      result.Append("</style>");
      result.Append("<script type=\"text/javascript\">");
      result.Append("\r\n\t\t\t\t\t\tfunction togle(anId){\r\n\t\t\t\t\t\t\tvar el = document.getElementById(anId);     \r\n\t\t\t\t\t\t\tif (el!=null){\r\n\t\t\t\t\t\t\t\tif (el.style.display=='none'){\r\n\t\t\t\t\t\t\t\t\tel.style.display='block'; \t    \r\n\t\t\t\t\t\t\t\t}else{\r\n\t\t\t\t\t\t\t\t\tel.style.display='none'; \t    \r\n\t\t\t\t\t\t\t\t}     \r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t  } \r\n\r\n\t\t\t\t\t\tfunction showAllTestClassesSummary(){\r\n\t\t\t\t\t\t\tvar tSummaryDetails = document.getElementById('tSummaryDetail');\r\n\t\t\t\t\t\t\tfor (var i=0;i<tSummaryDetails.rows.length;i++){\r\n\t\t\t\t\t\t\t\tr = tSummaryDetails.rows[i];\r\n\t\t\t\t\t\t\t\tr.style.display='block';\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\t");
      result.Append("</script>");
      result.Append("</head><body>");
      result.Append("<a name='__top' />");
      result.AppendFormat("<h3>{0}</h3>\r\n\t\t\t\t\t\t\t\t<div class='contents'>\r\n\t\t\t\t\t\t\t\t  <a href='#totals'>Totals</a>\r\n\t\t\t\t\t\t\t\t  |\r\n\t\t\t\t\t\t\t\t  <a href='#summary'>Summary</a>\r\n\t\t\t\t\t\t\t\t  |\r\n\t\t\t\t\t\t\t\t  <a href='#slower'>Slowers</a>\r\n\t\t\t\t\t\t\t\t  |\r\n\t\t\t\t\t\t\t\t  <a href='#detail'>Detail</a>\r\n\t\t\t\t\t\t\t\t  |\r\n\t\t\t\t\t\t\t\t  <a href='#envInfo'>Environment Information</a>\r\n\t\t\t\t\t\t\t\t</div>\r\n\t\t\t\t\t\t\t\t<br />\r\n\t\t\t\t\t\t\t\t<a name='totals' />", (object) run.Name);
    }

    private string GetCss()
    {
      return "\r\n\t\t\t\tbody, td, th {font:smaller Verdana;vertical-align:top;text-align:left}\r\n\t\t\t\tbody {margin:0px 0px 0px 15px}\r\n\t\t\t\thr {margin:0px 0px 0px -15px;border:solid 1pt #dcdcdc}\r\n\t\t\t\ttable {border-collapse:collapse;border:solid 1px #a9a9a9}\r\n\t\t\t\tth {background-color:#dcdcdc;border:solid 1px #a9a9a9;text-indent:2pt;font-weight:bolder}\r\n\t\t\t\ttd {border:solid 1px #a9a9a9}\r\n\t\t\t\tpre{font-size:8pt}\r\n\r\n\t\t\t\tspan.ok{background-color:lime;color:black;font-size:40%;color:lime;display:inline-block;}\r\n\t\t\t\tspan.ko{background-color:red;color:white;font-size:40%;color:red;display:inline-block;}\r\n\t\t\t\tspan.ignore{background-color:yellow;color:white;font-size:40%;color:yellow;display:inline-block;}\r\n\r\n\t\t\t\tp.testKo{\r\n\t\t\t\t\tcursor:hand;\r\n\t\t\t\t\twidth:40px;\r\n\t\t\t\t\theight:40px;\r\n\t\t\t\t\tbackground-image:url();\r\n\t\t\t\t\tbackground-repeat:no-repeat\r\n\t\t\t\t}                \r\n\t\t\t\tp.testOk{\r\n\t\t\t\t\tcursor:hand;\r\n\t\t\t\t\twidth:40px;\r\n\t\t\t\t\theight:40px;\r\n\t\t\t\t\tbackground-image:url();\r\n\t\t\t\t\tbackground-repeat:no-repeat\r\n\t\t\t\t}\r\n\t\t\t\tp.testIgnore{\r\n\t\t\t\t\tcursor:hand;\r\n\t\t\t\t\twidth:40px;\r\n\t\t\t\t\theight:40px;\r\n\t\t\t\t\tbackground-image:url();\r\n\t\t\t\t\tbackground-repeat:no-repeat\r\n\t\t\t   }\r\n\t\t\t\tdiv.barContainer{width:100%;border:solid 1px black}\r\n\t\t\t\tdiv.trace{font:100% Courier}\r\n\t\t\t\tdiv.border{border:dotted 1px #dcdcdc;padding:2px 2px 2px 2px}\r\n\t\t\t\tdiv.contents{border:dotted 1px #dcdcdc;padding:2px 2px 2px 2px;background-color:#efefef}\r\n\t\t\t\t\t\t\t\t\t";
    }

    private void WriteEnvironmentInfo(TestRunResult run, StringBuilder result)
    {
      result.Append("<br /><a name=\"envInfo\" /><table width='900px'><tr><th colspan=\"2\">TestRun Environment Information</th></tr>");
      result.AppendFormat("<tr><th align=\"right\">MachineName</th><td>{0}</td></tr>", (object) Enumerable.First<string>(run.Computers));
      result.Append("<tr><th align=\"right\">TestAssemblies</th><td>");
      foreach (AssemblyName assemblyName in run.Assemblies)
        result.Append(assemblyName.FullName + " <br />");
      result.Append("</td></tr>");
      result.AppendFormat("<tr><th align=\"right\">UserName</th><td>{0}</td></tr>", (object) run.UserName);
      result.AppendFormat("<tr><th align=\"right\">TRX File</th><td>{0}</td></tr>", (object) run.Name);
      result.Append("</table>");
    }

    private void WriteFooter(TestRunResult run, StringBuilder result)
    {
      result.Append("<hr style=\"border-style:dotted;color:#dcdcdc\"/><i style=\"width:100%;font:10pt Verdana;text-align:center;background-color:#dcdcdc\">The VSTS Test Results HTML Viewer. (c) <a href=\"http://blogs.msdn.com/rido\">rido</a>'2011</i>");
      result.Append("</body></html>");
    }

    private void WriteBody(TestRunResult run, StringBuilder result)
    {
      this.WriteSummary(run, result);
      this.WriteSummaryDetails(run, result);
      this.WriteSlowerMethods(run, result);
      foreach (IGrouping<string, TestMethodRun> grouping in Enumerable.GroupBy<TestMethodRun, string>(run.TestMethodRunList, (Func<TestMethodRun, string>) (m => m.TestClass)))
      {
        IGrouping<string, TestMethodRun> c = grouping;
        this.WriteClassResult(Enumerable.First<TestClassRun>((IEnumerable<TestClassRun>) run.TestClassList, (Func<TestClassRun, bool>) (tc => tc.FullName == c.Key)), result);
      }
    }

    private void WriteSlowerMethods(TestRunResult run, StringBuilder result)
    {
      result.Append("<a name='slower' /><h5>TOP 5 Slower Methods</h5><table id=\"tSlowerMethods\" border='0' width='900px'><tr><th>TestMethod</th><th colspan='2'>Status</th><th>Duration</th></tr>");
      foreach (TestMethodRun m in run.TopSlowerMethods)
        this.WriteTestMethodResult(m, result);
      result.Append("</table>");
    }

    private void WriteSummary(TestRunResult run, StringBuilder result)
    {
      result.AppendFormat("<table id='tMainSummary'  border='0' width='900px'>\r\n\t\t\t\t\t  <tr>\r\n\t\t\t\t\t\t<th>Percent</th>\r\n\t\t\t\t\t\t<th>Status</th>\r\n\t\t\t\t\t\t<th>TotalTests</th>\r\n\t\t\t\t\t\t<th>Passed</th>\r\n\t\t\t\t\t\t<th>Failed</th>\r\n\t\t\t\t\t\t   <th>Inconclusive</th>\r\n\t\t\t\t\t\t<th>TimeTaken</th>\r\n\t\t\t\t\t  </tr>\r\n\t\t\t\t\t  <tr>\r\n\t\t\t\t\t\t<td>{0}%</td>\r\n\t\t\t\t\t\t<td width='350px' style='vertical-align:middle;font-size:200%'>{1}</td>\r\n\t\t\t\t\t\t<td>{2}</td>\r\n\t\t\t\t\t\t<td>{3}</td>\r\n\t\t\t\t\t\t<td>{4}</td>\r\n\t\t\t\t\t\t<td>{5}</td>\r\n\t\t\t\t\t\t<td>{6}</td>\r\n\t\t\t\t\t  </tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t\t<br />", (object) run.TotalPercent, (object) this.CreateHtmlBars((I3ValueBar) run), (object) run.TotalMethods, (object) run.Passed, (object) run.Failed, (object) run.Inconclusive, (object) run.TimeTaken);
    }

    private string CreateHtmlBars(I3ValueBar run)
    {
      double percentOk = run.PercentOK;
      double percentKo = run.PercentKO;
      double percentIgnored = run.PercentIgnored;
      string str = "<div class=\"barContainer\">";
      if (percentOk != 0.0)
        str = str + string.Format("<span class=\"ok\"  style=\"width:{0}%\" title=\"Passed!\" >p</span>", (object) percentOk.ToString());
      if (percentKo != 0.0)
        str = str + string.Format("<span class=\"ko\"  style=\"width:{0}%\" title=\"Failed\">f</span>", (object) percentKo.ToString());
      if (percentIgnored != 0.0)
        str = str + string.Format("<span class=\"ignore\"  style=\"width:{0}%\" title=\"Failed\">f</span>", (object) percentIgnored.ToString());
      return str + "</div>";
    }

    private void WriteSummaryDetails(TestRunResult run, StringBuilder result)
    {
      result.Append("<table id='tSummaryDetail'  border='0' width='900px' >                              \r\n\t\t\t\t\t\t\t  <tr>\r\n\t\t\t\t\t\t\t\t<th colspan='7'>Failed TestClasses Summary (<a href='#' onclick='showAllTestClassesSummary()'> Show All</a> )</th>\r\n\t\t\t\t\t\t\t  </tr>\r\n\t\t\t\t\t\t\t  <tr>\r\n\t\t\t\t\t\t\t\t<th>Class Name</th>\r\n\t\t\t\t\t\t\t\t<th>Percent</th>\r\n\t\t\t\t\t\t\t\t<th>Status</th>\r\n\t\t\t\t\t\t\t\t<th>TestsPassed</th>\r\n\t\t\t\t\t\t\t\t<th>TestsFailed</th>\r\n\t\t\t\t\t\t\t\t<th>TestsIgnored</th>\r\n\t\t\t\t\t\t\t\t<th>Duration</th>\r\n\t\t\t\t\t\t\t  </tr>");
      foreach (TestClassRun testClassRun in run.TestClassList)
      {
        result.AppendFormat("<tr style=\"display:{0}\" >", testClassRun.Status == "Succeed" ? (object) "none" : (object) "block");
        result.AppendFormat("<td><a href='#{0}'>{1}</a></td>", (object) testClassRun.Name, (object) testClassRun.Name);
        result.AppendFormat("<td>{0}%</td>", (object) testClassRun.Percent);
        result.AppendFormat("<td width=\"80px\">{0}</td>", (object) this.CreateHtmlBars((I3ValueBar) testClassRun));
        result.AppendFormat("<td>{0}</td>", (object) testClassRun.Success);
        result.AppendFormat("<td>{0}</td>", (object) testClassRun.Failed);
        result.AppendFormat("<td>{0}</td>", (object) testClassRun.Ignored);
        result.AppendFormat("<td>{0}</td>", (object) testClassRun.Duration);
        result.AppendFormat("</tr>\r\n", new object[0]);
      }
      result.Append("</table>");
    }

    private void WriteClassResult(TestClassRun tcr, StringBuilder result)
    {
      result.Append("<br />");
      result.Append("<a name=\"detail\" />");
      result.AppendFormat("<h5></h5>\r\n\t\t\t\t\t\t\t\t  <a name='{0}' />\r\n\t\t\t\t\t\t\t\t  <table border='0' width='900px'>\r\n\t\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t\t  <th colspan='4'>\r\n\t\t\t\t\t\t\t\t\t\t<b>{0}</b>\r\n\t\t\t\t\t\t\t\t\t  </th>\r\n\t\t\t\t\t\t\t\t\t</tr>", (object) tcr.Name);
      foreach (TestMethodRun m in tcr.TestMethods)
        this.WriteTestMethodResult(m, result);
      result.Append("</table>\r\n");
    }

    private void WriteTestMethodResult(TestMethodRun m, StringBuilder result)
    {
      result.Append("<tr>\r\n");
      result.AppendFormat("<td>{0}.{1}</td>", (object) this.GetClassNameFromFullName(m.TestClass), (object) m.TestMethodName);
      switch (m.Status)
      {
        case "Failed":
          this.AppendFailed(m, result);
          break;
        case "Passed":
          this.AppendPassed(m, result);
          break;
        default:
          this.AppendIgnored(m, result);
          break;
      }
      result.AppendFormat("<td>{0}</td>", (object) m.Duration);
      result.Append("</tr>\r\n");
    }

    private string GetClassNameFromFullName(string fullName)
    {
      int num = fullName.IndexOf(' ');
      if (num > 0)
        return fullName.Substring(0, num - 1);
      else
        return fullName;
    }

    private void AppendMethodBullet(TestMethodRun m, StringBuilder result, string cssClass, string overColor, string outColor)
    {
      result.AppendFormat("<td><p class='{0}' title='Click to see the StackTrace' onmouseover='this.style.color=\"{1}\"' onmouseout='this.style.color=\"{2}\"' onclick=\"togle('{3}')\"></p></td>", (object) cssClass, (object) overColor, (object) outColor, (object) ((object) m).GetHashCode());
      this.AppendErrorInfo(m, result);
    }

    private void AppendIgnored(TestMethodRun m, StringBuilder result)
    {
      this.AppendMethodBullet(m, result, "testIgnore", "white", "yellow");
    }

    private void AppendPassed(TestMethodRun m, StringBuilder result)
    {
      this.AppendMethodBullet(m, result, "testOk", "green", "lime");
    }

    private void AppendFailed(TestMethodRun m, StringBuilder result)
    {
      this.AppendMethodBullet(m, result, "testKo", "orange", "red");
    }

    private void AppendErrorInfo(TestMethodRun m, StringBuilder result)
    {
      result.AppendFormat("<td width='100%'>{0} <br /> {1}\r\n\t\t\t\t\t\t\t\t\t<div id='{2}' class='trace'  style='display:none'>\r\n\t\t\t\t\t\t\t\t\t\t  <div class='border'>{3}<br />{4}<br />{5}<br />{6}</div>\r\n\t\t\t\t\t\t\t\t\t\t<pre  class='failureInfo' >{6}</pre>\r\n\t\t\t\t\t\t\t\t\t</div>            \r\n\t\t\t\t\t\t\t\t\t</td>", (object) m.Description, (object) m.ErrorInfo.Message, (object) ((object) m).GetHashCode(), (object) m.ErrorInfo.StdOut, m.ErrorInfo.StdErr, (object) m.ErrorInfo.Message, (object) m.ErrorInfo.StackTrace);
    }
  }
}
